package com.hzhc.controller.declare;

import com.hzhc.annotation.SysTemLog;
import com.hzhc.dto.UserDto;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.DeclareCarService;
import com.hzhc.service.VehicleReservationService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.DeclareCarVo;
import com.hzhc.vo.VehicleReservationVo;
import com.hzhc.vo.YwOrderCntrVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 卡口 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */

@RestController
@RequestMapping("/declareCarWeb")
@RequiredArgsConstructor
public class DeclareCarWebController {
    private final DeclareCarService declareCarService;
    private final VehicleReservationService vehicleReservationService;
    @Value("${autoTally}")
    private String autoTally;

    /**
     * 卡口进卡页面获取集装箱列表
     */
    @RequestMapping("/getGateYwOrderCntrList")
    @ResponseBody
    public ResultUtil<List<YwOrderCntrVo>> getGateYwOrderCntrList(@RequestBody DeclareCarVo vo) {
        List<YwOrderCntrVo> cntrSizes = declareCarService.getGateYwOrderCntrList(vo);
        return ResultUtil.build(cntrSizes, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卡口出卡页面获取集装箱列表
     */
    @RequestMapping("/getOutGateYwOrderCntrList")
    @ResponseBody
    public ResultUtil<List<DeclareCarVo>> getOutGateYwOrderCntrList(@RequestBody DeclareCarVo vo) {
        List<DeclareCarVo> declareCarVos = declareCarService.getOutGateYwOrderCntrList(vo);
        return ResultUtil.build(declareCarVos, ResultUtilCodeEnum.SUCCESS);
    }

    @RequestMapping("/getChooseData")
    @ResponseBody
    public ResultUtil<Map<String, Object>> getChooseData(@RequestBody VehicleReservationVo vo) {
        Map<String, Object> data = vehicleReservationService.getChooseData(vo);
        return ResultUtil.build(data, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卡口进卡
     */
    @PostMapping("/addJkInGateRet")
    @SysTemLog(operModul = "卡口管理-卡口进卡登记",operType = "卡口进卡登记",operDesc = "卡口进卡登记功能",modular="PC")
    public ResultUtil<T> addJkInGateRet(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addJkInGateRet(vo);
    }

    /**
     * 保存出卡数据
     *
     * @param vo
     * @return
     */
    @PostMapping("/addCkInGateRet")
    @SysTemLog(operModul = "卡口管理-卡口出卡登记", operType = "出卡登记", operDesc = "卡口出卡登记功能", modular = "PC")
    public ResultUtil<T> addCkInGateRet(HttpServletRequest request,@RequestBody  DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addCkInGateRet(vo);
    }

    /**
     * 根据车牌号获取未完成的过卡的数据
     *
     * @param vo
     * @return
     */
    @PostMapping("/getNotFinishedWeigh")
    public ResultUtil<DeclareCarVo> getNotFinishedWeigh(@RequestBody DeclareCarVo vo) {
        DeclareCarVo declareCarVo = declareCarService.getNotFinishedWeigh(vo);
        return ResultUtil.build(declareCarVo, ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 散改集卡口进卡
     */
    @PostMapping("/addSgjInGateRet")
    @SysTemLog(operModul = "卡口管理-卡口进卡登记",operType = "卡口进卡登记",operDesc = "卡口进卡登记功能",modular="PC")
    public ResultUtil<T> addSgjInGateRet(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addSgjInGateRet(vo);
    }

    /**
     * 散改集卡口出卡
     */
    @PostMapping("/addSgjOutGateRet")
    @SysTemLog(operModul = "卡口管理-卡口进卡登记",operType = "卡口进卡登记",operDesc = "卡口进卡登记功能",modular="PC")
    public ResultUtil<T> addSgjOutGateRet(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addSgjOutGateRet(vo);
    }

    /**
     * 散改集 再次提箱 生成一个新的卡口数据
     */
    @PostMapping("/addSgjNewInGateRet")
    public ResultUtil<T> addSgjNewInGateRet(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addSgjNewInGateRet(vo);
    }

    @RequestMapping("/getChooseDataByCarNo")
    @ResponseBody
    public ResultUtil<Map<String, Object>> getChooseDataByCarNo(@RequestBody VehicleReservationVo vo) {
        Map<String, Object> data = vehicleReservationService.getChooseDataByCarNo(vo);
        return ResultUtil.build(data, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卡口进卡
     */
    @PostMapping("/addNewJkInGateRet")
    @SysTemLog(operModul = "卡口管理-卡口进卡登记",operType = "卡口进卡登记",operDesc = "卡口进卡登记功能",modular="PC")
    public ResultUtil<T> addNewJkInGateRet(HttpServletRequest request, @RequestBody DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addNewJkInGateRet(vo,autoTally);
    }


    /**
     * 获取出卡车牌号
     *
     * @param vo
     * @return
     */
    @PostMapping("/getCkCarNo")
    public ResultUtil<List<DeclareCarVo>> getCkCarNo(@RequestBody DeclareCarVo vo) {
        List<DeclareCarVo> carInfoVos = declareCarService.getCkCarNo(vo);
        return ResultUtil.build(carInfoVos, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 保存出卡数据
     *
     * @param vo
     * @return
     */
    @PostMapping("/addNewCkInGateRet")
    @SysTemLog(operModul = "卡口管理-卡口出卡登记", operType = "出卡登记", operDesc = "卡口出卡登记功能", modular = "PC")
    public ResultUtil<T> addNewCkInGateRet(HttpServletRequest request,@RequestBody  DeclareCarVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setCreateTime(LocalDateTime.now());
        vo.setUserName(user.getRealname());
        vo.setUserId(user.getId());
        return declareCarService.addNewCkInGateRet(vo);
    }
}
